#include "DB.h"
#include "lib.h"

class Little
{
public:
    int i = 0;
    std::string value = "";
    _CLASSS_JSON_DEFAULT(i, value)// 对象序列化
};

class Item
{
public:
    int _ID;
    std::string name;
    std::vector<string> list;
    Little little;

    _DBF(field, name, list, little)// 数据库序列化变量 field
    _CLASSS_JSON_DEFAULT(_ID, name, list, little)// 对象序列化

    Item() {}
};

void test_database(void)
{

    Item i, j;
    i.name = "scut";
    i.list.push_back("h");
    i.list.push_back("l");
    sql::Database db;

    // 初始化数据库
    std::string db_name = "test.db";
    std::string tb_name = "Item";
    db.open(db_name);
    Table tb(db.getHandle(), tb_name, i.field);
    if (tb.exists() == false)
    {
        tb.create();
        printf("[数据库] 创建表 = %s", tb_name.c_str());
    }
    sql::Record r(tb.fields());
    std::string str = DB::class2record(&i, &r);

    // add
    // tb.addRecord(&r);

    // find
    string header("SELECT * FROM \"Item\" WHERE \"_ID\" = 2;"); // select * from hour order by date desc limit 1;
    tb.query(header);
    if (tb.recordCount() > 0)
    {
        Record *pr = tb.getRecord(0);
        string result = DB::record2class(pr, j);
        printf("[数据库] [0] = %s  \n", result.c_str());
    }

    header = "SELECT * FROM \"Item\" WHERE \"_ID\" = 1;"; // select * from hour order by date desc limit 1;
    tb.query(header);
    if (tb.recordCount() > 0)
    {
        Record *pr = tb.getRecord(0);
        string result = DB::record2class(pr, j);
        printf("[数据库] [0] = %s  \n", result.c_str());
    }
    db.close();
}